package com.lguplus.servicepoi.gisp;

import com.lguplus.servicepoi.util.Util;

public class MessageHeader {

	private char soh = 0x01;
	
	private String	version;
	private char 	mainConn;
	private char	msgType;
	private int	gpContentType;
	private String transactionId;
	private int    bodyLen;
	private int    binLen;
	private String binType;
	private int    sequence;

	public MessageHeader() {
		this.soh = 0x01;
		this.version = "";
		this.mainConn = 0x00;
		this.msgType = 0x11;
		this.gpContentType = 1102;
		this.transactionId = "";
		this.bodyLen = 0;
		this.binLen = 0;
		this.binType = "PNG";
		this.sequence = 0;
	}
	
	public byte[] getHeaderBytes() throws Exception{
		byte[] b = new byte[49];

		b[0] = 0x01;
		
		System.arraycopy(this.version.getBytes(), 0, b, 1, this.version.getBytes().length > 6?6:this.version.getBytes().length);
		b[7] = (byte)this.mainConn;
		b[8] = (byte)this.msgType;
		System.arraycopy(Util.Int2byte(this.gpContentType), 0, b, 9, 4);
		System.arraycopy(this.transactionId.getBytes(), 0, b, 13, this.transactionId.getBytes().length > 20?20:this.transactionId.getBytes().length);
		System.arraycopy(Util.Int2byte(this.bodyLen), 0, b, 33, 4);
		System.arraycopy(Util.Int2byte(this.binLen), 0, b, 37, 4);
		System.arraycopy(this.binType.getBytes(), 0, b, 41, this.binType.getBytes().length > 4?4:this.binType.getBytes().length);
		System.arraycopy(Util.Int2byte(this.sequence), 0, b, 45, 4);
		
		return b;
	}
	
	public void setHeaderBytes(byte[] b) throws Exception{
		this.setVersion(Util.GetSubString(b, 0, 6));
		this.setMainConn((char) b[6]);
		this.setMsgType((char) b[7]);
		this.setGpContentType(Util.GetSubInt(b, 8));
		this.setTransactionId(Util.GetSubString(b, 12, 20));
		this.setBodyLen(Util.GetSubInt(b, 32));
		this.setBinLen(Util.GetSubInt(b, 36));
		this.setBinType(Util.GetSubString(b, 40, 4));
		this.setSequence(Util.GetSubInt(b, 44));
	}

	public char getSoh() {
		return soh;
	}

	public void setSoh(char soh) {
		this.soh = soh;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public char getMainConn() {
		return mainConn;
	}

	public void setMainConn(char mainConn) {
		this.mainConn = mainConn;
	}

	public char getMsgType() {
		return msgType;
	}

	public void setMsgType(char msgType) {
		this.msgType = msgType;
	}

	public int getGpContentType() {
		return gpContentType;
	}

	public void setGpContentType(int gpContentType) {
		this.gpContentType = gpContentType;
	}

	public String getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(String transactionId) {
		this.transactionId = transactionId;
	}

	public int getBodyLen() {
		return bodyLen;
	}

	public void setBodyLen(int bodyLen) {
		this.bodyLen = bodyLen;
	}

	public int getBinLen() {
		return binLen;
	}

	public void setBinLen(int binLen) {
		this.binLen = binLen;
	}

	public String getBinType() {
		return binType;
	}

	public void setBinType(String binType) {
		this.binType = binType;
	}

	public int getSequence() {
		return sequence;
	}

	public void setSequence(int sequence) {
		this.sequence = sequence;
	}
	
	
	
}
